Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

normalizr

Package Overview
Dependencies
Maintainers
2
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

normalizr

Normalizes and denormalizes JSON according to schema for Redux and Flux applications

  • 3.6.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is normalizr?

Normalizr is a powerful library for normalizing nested JSON data. It helps in transforming complex nested data structures into a flat structure, making it easier to manage and work with in applications, especially in state management scenarios.

What are normalizr's main functionalities?

Define Schemas

Normalizr allows you to define schemas for your data. In this example, we define schemas for users, comments, and articles, where comments have a relationship with users, and articles have relationships with both users and comments.

const { schema } = require('normalizr');

const user = new schema.Entity('users');
const comment = new schema.Entity('comments', {
  commenter: user
});
const article = new schema.Entity('articles', {
  author: user,
  comments: [comment]
});

Normalize Data

Once schemas are defined, you can use the `normalize` function to transform your nested data into a normalized form. This example shows how to normalize a nested JSON object representing an article with an author and comments.

const { normalize } = require('normalizr');

const originalData = {
  id: '123',
  author: {
    id: '1',
    name: 'Paul'
  },
  title: 'My awesome blog post',
  comments: [
    {
      id: '324',
      commenter: {
        id: '2',
        name: 'Nicole'
      }
    }
  ]
};

const normalizedData = normalize(originalData, article);
console.log(JSON.stringify(normalizedData, null, 2));

Denormalize Data

Normalizr also provides a `denormalize` function to convert normalized data back into its original nested form. This example demonstrates how to denormalize data using the previously defined schemas.

const { denormalize } = require('normalizr');

const normalizedData = {
  result: '123',
  entities: {
    articles: {
      '123': { id: '123', author: '1', title: 'My awesome blog post', comments: ['324'] }
    },
    users: {
      '1': { id: '1', name: 'Paul' },
      '2': { id: '2', name: 'Nicole' }
    },
    comments: {
      '324': { id: '324', commenter: '2' }
    }
  }
};

const denormalizedData = denormalize('123', article, normalizedData.entities);
console.log(JSON.stringify(denormalizedData, null, 2));

Other packages similar to normalizr

Keywords

FAQs

Package last updated on 19 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc